package com.xxwy.segmenttree.practice.leetcode;

/**
 * @author xxwy
 */
public class NumArray307 {
    class NumArray {

        private int[] sum;
        private int[] data;

        public NumArray(int[] nums) {
            data = new int[nums.length];
            for (int i = 0; i < nums.length; i++) {
                data[i] = nums[i];
            }
            sum = new int[nums.length+1];
            sum[0] = 0;
            for (int i = 1; i < sum.length; i++) {
                sum[i] = nums[i-1] + sum[i-1];
            }
        }

        public void update(int index, int val) {
            data[index] = val;
            for (int j = index+1; j < sum.length; j--) {
                sum[j]= sum[j-1] + data[j-1];
            }
        }

        public int sumRange(int i, int j) {
            return sum[j+1] - sum[i];
        }
    }
}
